package com.gitee.mars.tourserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.tourserver.entity.TourHotel;
import com.gitee.mars.tourserver.manager.TourClawerManager;
import com.gitee.mars.tourserver.request.TravelStrategyRequest;
import com.gitee.mars.tourserver.response.base.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gitee.mars.tourserver.service.ITravelStrategyService;
import com.gitee.mars.tourserver.mapper.TravelStrategyMapper;
import com.gitee.mars.tourserver.entity.TravelStrategy;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;


/**
 * TravelStrategy 实现层
 *
 * @Author wq
 * @Date 2021-04-14
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class TravelStrategyServiceImpl extends ServiceImpl<TravelStrategyMapper, TravelStrategy> implements ITravelStrategyService {

    @Resource
    private TourClawerManager clawerManager;

    @Override
    public void acquire() {
        List<TravelStrategy> travelStrategy = clawerManager.getTravelStrategy();
        travelStrategy.forEach(this::save);
    }

    @Override
    public PageInfo<TravelStrategy> list(TravelStrategyRequest request) {
        IPage<TravelStrategy> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<TravelStrategy> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(request.getDescription())) {
            wrapper.like(TravelStrategy::getDescription, request.getDescription());
        }
        if (StringUtils.isNotEmpty(request.getTravelTheWay())) {
            wrapper.like(TravelStrategy::getTravelTheWay, request.getTravelTheWay());
        }
        if (StringUtils.isNotEmpty(request.getTravelTrip())) {
            wrapper.like(TravelStrategy::getTravelTrip, request.getTravelTrip());
        }
        wrapper.orderByDesc(TravelStrategy::getCreateTime);
        List<TravelStrategy> list = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(list)) {
            return new PageInfo<>(page.getTotal(), list);
        }
        return null;
    }
}